//
//  averageWidthCollectionViewController.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 8/10/15.
//  Copyright (c) 2015 dvlproad. All rights reserved.
//

#import "TSCellMoveCollectionViewController.h"
#import <CQDemoKit/CJUIKitToastUtil.h>
#import <CQDemoKit/CQTSButtonFactory.h>
#import <UICollectionViewCJHelper/UICollectionView+CJSelect.h>

#import "TSCellMoveCollectionView.h"
#import "TSCellMoveCollectionView+Move.h"

@interface TSCellMoveCollectionViewController () {
    
}
@property (nonatomic, strong) TSCellMoveCollectionView *collectionView;
@property (nonatomic, assign, readonly) BOOL isChoosing;
@end

@implementation TSCellMoveCollectionViewController

- (BOOL)automaticallyAdjustsScrollViewInsets {
    return NO;
}

- (void)__chooseBarAction:(UIBarButtonItem *)chooseBarButtonItem {
    _isChoosing = !self.isChoosing;
    
    if (_isChoosing) {
        chooseBarButtonItem.title = @"取消";
        
        [self.collectionView beginShake];
        [self.collectionView addMoveGRWithCellMoveErrorBlock:^(NSString * _Nonnull erorMessage) {
            [CJUIKitToastUtil showMessage:erorMessage];
        }];
    } else {
        chooseBarButtonItem.title = @"编辑";
        
        [self.collectionView endShake];
        [self.collectionView removeMoveGR];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Cell的Move";
    
    UIBarButtonItem *chooseBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(__chooseBarAction:)];
    _isChoosing = NO;
    self.navigationItem.rightBarButtonItems = @[chooseBarButtonItem];
    

    TSCellMoveCollectionView *collectionView = [[TSCellMoveCollectionView alloc] init];
    collectionView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(120);
        make.left.mas_equalTo(self.view).mas_offset(10);
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(20);
    }];
    self.collectionView = collectionView;
    
    NSMutableArray<NSString *> *dataModels = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < 20; i++) {
        NSString *dataModel = [NSString stringWithFormat:@"我是%zd", i];
        [dataModels addObject:dataModel];
    }
    self.collectionView.dataModels = dataModels;
    [self.collectionView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
